import { PageQueryFormType, QueryFormType } from '@/types/queryform'
import { PromiseResponse, PromiseResponsePage } from '@/types/type'
import createAxios, { request } from '@/utils/request'
import { AxiosResponse } from 'axios'
import { ControllerType } from './types'

export class ProTableApi {
    public controllerUrl: ControllerType
    public actionUrl

    constructor(controllerUrl: ControllerType) {
        this.controllerUrl = controllerUrl
        this.actionUrl = new Map([
            ['getAll', `/${controllerUrl}/getAll`],
            ['getPage', `/${controllerUrl}/getPage`],
            ['getById', `/${controllerUrl}/getById`],
            ['getByIds', `/${controllerUrl}/getByIds`],
            ['import', `/${controllerUrl}/import`],
            ['export', `/${controllerUrl}/export`],
            ['exportTemplate', `/${controllerUrl}/exportTemplate`],
            ['add', `/${controllerUrl}/add`],
            ['edit', `/${controllerUrl}/update`],
            ['enabled', `/${controllerUrl}/enabled`],
            ['assignPermissions', `/${controllerUrl}/assignPermissions`],
            ['delete', `/${controllerUrl}/delete`],
            ['batchDelete', `/${controllerUrl}/batchDelete`],
            ['sortable', `/${controllerUrl}/sortable`],
        ])
    }
    /**
     * 获取所有
     * @param queryForm 查询表单
     * @returns 所有数据
     */
    getAll(queryForm?: QueryFormType<any>): PromiseResponse<any[]> {
        return request(this.actionUrl.get('getAll') as string, 'GET', {}, queryForm)
    }
    /**
     * 分页获取
     * @param pageQueryForm 分页查询表单
     * @returns 分页数据
     */
    getPage(pageQueryForm?: PageQueryFormType<any>): PromiseResponsePage<any[]> {
        return request(this.actionUrl.get('getPage') as string, 'GET', {}, pageQueryForm)
    }
    /**
     * 根据id获取数据
     * @returns
     */
    getById(id: number) {
        return request(this.actionUrl.get('getById') as string, 'GET', {}, { id })
    }
    /**
     * 根据id数组获取数据
     * @param ids 数据Id数组
     * @returns
     */
    getByIds(ids: number[]) {
        return request(this.actionUrl.get('getById') as string, 'GET', {}, { ids })
    }
    /**
     * 批量导入
     * @param data 要导入的数组
     * @returns
     */
    import(data: any) {
        return request(this.actionUrl.get('import') as string, 'POST', data)
    }
    /**
     * 导出菜单
     * @param ids 数据id数组
     * @returns 导出的excel文件
     */
    export(ids: number[]): Promise<AxiosResponse<any>> {
        return createAxios({ responseType: 'blob' }).request({ url: this.actionUrl.get('export'), method: 'POST', data: { ids } })
    }
    /**
     * 导出模版
     * @returns 导出的excel模版文件
     */
    exportTemplate(): Promise<AxiosResponse<any>> {
        return createAxios({ responseType: 'blob' }).request({ url: this.actionUrl.get('exportTemplate'), method: 'POST' })
    }
    /**
     * 添加或编辑
     * @param data 添加或者更新表单, 有id是更新，没有id是添加
     * @returns
     */
    addOrEdit(data: any): PromiseResponse<boolean> {
        if (data.id) {
            return request(this.actionUrl.get('edit') as string, 'PUT', data)
        } else {
            return request(this.actionUrl.get('add') as string, 'POST', data)
        }
    }
    /**
     * 切换数据状态
     * @param id 数据id
     * @returns
     */
    enabled(id: number): PromiseResponse<boolean> {
        return request(this.actionUrl.get('enabled') as string, 'PUT', {}, { id })
    }
    /**
     * 分配权限
     * @param id 菜单Id
     * @param ids 接口Id数组
     * @returns
     */
    assignPermissions(id: number, ids: number[]): PromiseResponse<boolean> {
        return request(this.actionUrl.get('assignPermissions') as string, 'POST', { id, ids })
    }
    /**
     * 删除
     * @param id 数据id
     * @returns
     */
    delete(id: number): PromiseResponse<boolean> {
        return request(this.actionUrl.get('delete') as string, 'DELETE', {}, { id })
    }
    /**
     * 批量删除
     * @param ids 数据id数组
     * @returns
     */
    batchDelete(ids: number[]): PromiseResponse<boolean> {
        return request(this.actionUrl.get('batchDelete') as string, 'DELETE', { ids }, {})
    }

    sortable(data: any) {
        return request(this.actionUrl.get('sortable') as string, 'PUT', data)
    }
}
